<script setup lang="ts">
import { Plus } from "@element-plus/icons-vue";
import { useStuctureStore } from "./store.ts";
import { is_null_obj } from "@/func.js";
import { ElButton } from "element-plus";

import type { Stucture } from "./store.ts";

const stucture = useStuctureStore();
const props = defineProps({
  type: {
    type: String,
    default: "add",
  },
});

const show = ref(false);
const save = () => {
  if (props.type === "add") {
    stucture.add_stucture();
  } else if (props.type === "edit") {
    stucture.edit_stucture();
  }
  show.value = false;
};

// 要添加的上级对象(添加时点击的行)
const pid = ref();
const _id = ref();
const open = () => {
  if (props.type === "add") {
    _id.value = stucture.stucture._id;
    pid.value = stucture.stucture.id;
    stucture.stucture = {} as Stucture;
    console.log(pid.value);
    console.log(_id.value);

    stucture.set_stucture({ p_id: _id.value });
    stucture.set_stucture({ pid: pid.value });
  }
  show.value = true;
};
const close = () => {
  show.value = false;
};
</script>

<template>
  <el-button
    :disabled="is_null_obj(stucture.stucture2)"
    v-if="type === 'add'"
    :type="'success'"
    :icon="Plus"
    @click="open"
  >
    新增
  </el-button>
  <el-button
    :disabled="is_null_obj(stucture.stucture2)"
    v-if="type === 'edit'"
    :type="'primary'"
    :icon="Plus"
    @click="open"
  >
    编辑
  </el-button>

  <r-dialog v-model="show" width="500">
    <template #header>
      <div class="text-xl" v-if="type === 'add'">+新增 组织结构</div>
      <div class="text-xl" v-if="type === 'edit'">编辑 组织结构</div>
    </template>

    <template #default>
      <div class="w-[60%] m-auto">
        <el-form>
          <el-form-item label="名称">
            <el-input v-model="stucture.stucture.name" />
          </el-form-item>
          <el-form-item label="编号">
            <el-input v-model="stucture.stucture.code" />
          </el-form-item>
          <el-form-item label="负责人">
            <el-input v-model="stucture.stucture.fuzename" />
          </el-form-item>
          <el-form-item label="上级ID">
            <el-input v-model="stucture.stucture.pid" />
          </el-form-item>
          <el-form-item label="序号">
            <el-input v-model="stucture.stucture.sortnum" />
          </el-form-item>
        </el-form>
      </div>
    </template>

    <template #footer>
      <el-button size="small" type="primary" @click="save">确认</el-button>
      <el-button size="small" @click="close">取消</el-button>
    </template>
  </r-dialog>
</template>
